﻿package Gameplay
{	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.geom.Point;
	import Gameplay.Events.MarkFailedEvent;

	public class Mark extends MovieClip
	{
		private const UP:Number = 40;		
		private const DOWN:Number = 370;
		private const HIT:Number = 330;		
		private var FID:int;
		private var FStrip:TrackStrip;
		private var FChecked:Boolean;
		private var FParentTrack:AbstractTrack;
		
		public function Mark()
		{
			FID = -1;
			visible = false;
			FChecked = false;
			addEventListener(Event.ENTER_FRAME, OnEnterFrame);
			mouseEnabled = mouseChildren = false;
		}
		
		private function OnEnterFrame(e:Event):void		
		{
			//y++;			
			var vY = parent.localToGlobal(new Point(x, y)).y;
			if(!visible)
			{
				if(vY > UP)
				{
					visible = true;
				}
			}
			else if(vY > DOWN)
			{
				if(!FChecked && FParentTrack)
				{					
					var vEvent:MarkFailedEvent = new MarkFailedEvent();
					vEvent.StripID = FStrip.ID;
					FParentTrack.dispatchEvent(vEvent);
				}
				
				if(!FChecked)
					FStrip.StepID();
				
				parent.removeChild(this);
				removeEventListener(Event.ENTER_FRAME, OnEnterFrame);
			}
		}
		
		public function CheckHit():Boolean
		{
			if(!FChecked)
			{
				var vY = parent.localToGlobal(new Point(x, y)).y;
				FChecked = ((vY > HIT) && (vY < DOWN));
				if(FChecked)
				{
					FStrip.StepID();
					removeEventListener(Event.ENTER_FRAME, OnEnterFrame);
					parent.removeChild(this);					
				}
				return FChecked;				
			}
			else
				return false;			
		}
		
		public function get ID():int
		{
			return FID;
		}
		
		public function set ID(AID:int):void
		{
			FID = AID;
		}
		
		public function set Strip(AStrip:TrackStrip):void
		{
			FStrip = AStrip;
		}
		
		public function set ParentTrack(ATrack:AbstractTrack):void
		{
			FParentTrack = ATrack;
		}
	}	
}
